/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.UserFavoritesPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  UserFavoritesPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class UserFavoritesSqlProvider {
	public static final String TABLE_NAME = "user_favorites";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param userFavorites
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserFavoritess(final UserFavoritesPo userFavorites) {
		return new CustomerSQL() {{
			SELECT("create_time,creator_id,id,main_image,price_original,price_promotional,update_time,updater_id,user_id,user_name,ware_id,ware_name ");
			FROM(TABLE_NAME);
			
			if (null != userFavorites.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getMainImage())) {
				WHERE("main_image=#{mainImage}");
			}
			if (null != userFavorites.getPriceOriginal()) {
				WHERE("price_original=#{priceOriginal}");
			}
			if (null != userFavorites.getPricePromotional()) {
				WHERE("price_promotional=#{pricePromotional}");
			}
			if (null != userFavorites.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != userFavorites.getUserId()) {
				WHERE("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getUserName())) {
				WHERE("user_name=#{userName}");
			}
			if (null != userFavorites.getWareId()) {
				WHERE("ware_id=#{wareId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getWareName())) {
				WHERE("ware_name=#{wareName}");
			}
			if(StringUtils.isNotBlank(userFavorites.getSortField())) {
				ORDER_BY(userFavorites.getSortField() + (StringUtils.isNotBlank(userFavorites.getSortMethod()) ? "desc" : userFavorites.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != userFavorites.getStart() && null != userFavorites.getSize()) {
				LIMIT(userFavorites.getStart(), userFavorites.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param userFavorites
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserFavoritesCount(final UserFavoritesPo userFavorites) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (null != userFavorites.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getMainImage())) {
				WHERE("main_image=#{mainImage}");
			}
			if (null != userFavorites.getPriceOriginal()) {
				WHERE("price_original=#{priceOriginal}");
			}
			if (null != userFavorites.getPricePromotional()) {
				WHERE("price_promotional=#{pricePromotional}");
			}
			if (null != userFavorites.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != userFavorites.getUserId()) {
				WHERE("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getUserName())) {
				WHERE("user_name=#{userName}");
			}
			if (null != userFavorites.getWareId()) {
				WHERE("ware_id=#{wareId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getWareName())) {
				WHERE("ware_name=#{wareName}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserFavoritesById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param userFavorites
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addUserFavorites(final UserFavoritesPo userFavorites) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (null != userFavorites.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getMainImage())) {
				VALUES("main_image","#{mainImage}");
			}
			if (null != userFavorites.getPriceOriginal()) {
				VALUES("price_original","#{priceOriginal}");
			}
			if (null != userFavorites.getPricePromotional()) {
				VALUES("price_promotional","#{pricePromotional}");
			}
			if (null != userFavorites.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
			if (null != userFavorites.getUserId()) {
				VALUES("user_id","#{userId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getUserName())) {
				VALUES("user_name","#{userName}");
			}
			if (null != userFavorites.getWareId()) {
				VALUES("ware_id","#{wareId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getWareName())) {
				VALUES("ware_name","#{wareName}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param userFavorites
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateUserFavorites(final UserFavoritesPo userFavorites) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (null != userFavorites.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getMainImage())) {
				SET("main_image=#{mainImage}");
			}
			if (null != userFavorites.getPriceOriginal()) {
				SET("price_original=#{priceOriginal}");
			}
			if (null != userFavorites.getPricePromotional()) {
				SET("price_promotional=#{pricePromotional}");
			}
			if (null != userFavorites.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			if (null != userFavorites.getUserId()) {
				SET("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getUserName())) {
				SET("user_name=#{userName}");
			}
			if (null != userFavorites.getWareId()) {
				SET("ware_id=#{wareId}");
			}
			if (StringUtils.isNotBlank(userFavorites.getWareName())) {
				SET("ware_name=#{wareName}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delUserFavorites(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
